/*
 * IFT615 - TP2
 * Session d'hiver 2009
 *
 * Université de Sherbrooke
 * Département d'informatique
 *
 * Created on 4 février 2008, 19:36
 */



import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import javax.swing.JFileChooser;

/**
 *
 * @author  Eric Beaudry
 */
public class SerpentsEchelles extends javax.swing.JFrame {
    
    PlateauDisplay  display;
    Plateau         plateau;
    PolitiqueSimple politique;
    JFileChooser    fileChooser = new JFileChooser(new File(".").getAbsoluteFile());
    
    /** Creates new form SerpentsEchelles */
    public SerpentsEchelles() {
        initComponents();
        
        plateau = Plateau.generate(200, 1, 20, 8, 40);
        //politique = GenPolitique1.generate(plateau);
        display = new PlateauDisplay();
        display.setPlateau(plateau);
        //display.setPolitique(politique);
        getContentPane().add(display, java.awt.BorderLayout.CENTER);
    }
    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        newPlateauButton = new javax.swing.JButton();
        chargerPlateauButton = new javax.swing.JButton();
        sauverPlateauButton = new javax.swing.JButton();
        jButton1 = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("IFT615/TP2 - Serpents et échelles");

        jPanel1.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED));

        newPlateauButton.setText("Nouveau plateau");
        newPlateauButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                newPlateauButtonActionPerformed(evt);
            }
        });
        jPanel1.add(newPlateauButton);

        chargerPlateauButton.setText("Charger plateau");
        chargerPlateauButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                chargerPlateauButtonActionPerformed(evt);
            }
        });
        jPanel1.add(chargerPlateauButton);

        sauverPlateauButton.setText("Sauver plateau");
        sauverPlateauButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                sauverPlateauButtonActionPerformed(evt);
            }
        });
        jPanel1.add(sauverPlateauButton);

        jButton1.setText("Charger politique");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });
        jPanel1.add(jButton1);

        getContentPane().add(jPanel1, java.awt.BorderLayout.PAGE_START);

        java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
        setBounds((screenSize.width-666)/2, (screenSize.height-468)/2, 666, 468);
    }// </editor-fold>//GEN-END:initComponents

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        if(fileChooser.showOpenDialog(this) == JFileChooser.APPROVE_OPTION){
            try{
                FileReader in = new FileReader(fileChooser.getSelectedFile());
                politique = PolitiqueSimple.load(in);
                in.close();
                display.setPolitique(politique);
            }catch(Exception e){
                e.printStackTrace();
            }
        }
    }//GEN-LAST:event_jButton1ActionPerformed

    private void chargerPlateauButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_chargerPlateauButtonActionPerformed
        if(fileChooser.showOpenDialog(this) == JFileChooser.APPROVE_OPTION){
            try{
                FileReader in = new FileReader(fileChooser.getSelectedFile());
                plateau = Plateau.load(in);
                in.close();
                display.setPlateau(plateau);
            }catch(Exception e){
                e.printStackTrace();
            }
        }
}//GEN-LAST:event_chargerPlateauButtonActionPerformed

    private void sauverPlateauButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_sauverPlateauButtonActionPerformed
        if(fileChooser.showSaveDialog(this) == JFileChooser.APPROVE_OPTION){
            try{
                FileWriter out = new FileWriter(fileChooser.getSelectedFile());
                Plateau.save(plateau, out);
                out.close();
            }catch(Exception e){
                e.printStackTrace();
            }
        }
    }//GEN-LAST:event_sauverPlateauButtonActionPerformed

    private void newPlateauButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_newPlateauButtonActionPerformed
        generateOptionDialog.setVisible(true);
        plateau = Plateau.generate(Integer.parseInt(generateOptionDialog.nbCasesTF.getText()),
                                   Integer.parseInt(generateOptionDialog.nbEchellesTF.getText()),
                                   Integer.parseInt(generateOptionDialog.gainMaxTF.getText()),
                                   Integer.parseInt(generateOptionDialog.nbSerpentsTF.getText()),
                                   Integer.parseInt(generateOptionDialog.perteMaxTF.getText())
                                   );
        display.setPlateau(plateau);
    }//GEN-LAST:event_newPlateauButtonActionPerformed
    
    private GenerateOptionsDialog generateOptionDialog = new GenerateOptionsDialog(this, true);
    
    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new SerpentsEchelles().setVisible(true);
            }
        });
    }
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton chargerPlateauButton;
    private javax.swing.JButton jButton1;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JButton newPlateauButton;
    private javax.swing.JButton sauverPlateauButton;
    // End of variables declaration//GEN-END:variables
    
}
